import { createRouter, createWebHistory } from "vue-router";
import Layout from "../views/layout/Layout.vue";
//NProgress 配置 进度条
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import login from "@/views/Login.vue";
NProgress.configure({ showSpinner: false })

const routes = [
  {
    path: '/login',
    component: () => import("../views/Login")
  },
  {
    path: "/",  // URL
    name: "首页",
    component: Layout, // 引用组件
    redirect: '/dashboard',  // 访问首页跳转到仪表盘
    children: [
      {
        path: '/dashboard',
        name: "仪表盘",
        icon: "HomeFilled",
        meta:{title:"仪表盘"},
        component: () => import('../views/dashborad/Dashboard')
      }
    ]
  },

  {
    path: "/host",
    name: "资源管理",
    icon: "Platform",
    // 导入组件方式2：当路由被访问时才会加载组件（惰性）
    component: Layout,
    children: [
      {
        path: '/host/idc',
        name: "机房管理",
        meta:{title:"机房管理"},
        component: () => import('@/views/cmdb/idc/Idc')
      },
      {
        path: '/host/server_group',
        name: "主机分组",
        meta:{title:"主机分组"},
        component: () => import('@/views/cmdb/servergroup/ServerGroup')
      },
      {
        path: '/host/server',
        name: "主机管理",
        meta:{title:"主机管理"},
        component: () => import('@/views/cmdb/server/Server')
      },
    ]
  },
  {
    path: "/release",
    name: "应用发布",
    icon: "Menu",
    // 导入组件方式2：当路由被访问时才会加载组件（惰性）
    component: Layout,
    children: [
      {
        path: '/release/env',
        name: "环境管理",
        meta:{title:"环境管理"},
        component: () => import('@/views/app_release/env/Env')
      },
      {
        path: '/release/project',
        name: "项目管理",
        meta:{title:"项目管理"},
        component: () => import('@/views/app_release/project/Project')
      },
      {
        path: '/release/app',
        name: "应用管理",
        meta:{title:"应用管理"},
        component: () => import('@/views/app_release/app/App')
      },
      {
        path: '/release/apply',
        name: "发布申请",
        meta:{title:"发布申请"},
        component: () => import('@/views/app_release/apply/Apply.vue')
      },
    ]
  },
  {
    path: "/task",
    name: "任务管理",
    icon: "AlarmClock",
    component: Layout,
    children: [
       {
        path: '/task/cron',
        name: "定时任务",
        meta:{title:"定时任务"},
        component: () => import('@/views/task_manage/cron/Cron')
      },
      {
        path: '/task/list',
        name: "任务列表",
        meta:{title:"任务列表"},
        component: () => import('@/views/task_manage/task/Task')
      },
      {
        path: '/task/record',
        name: "任务日志",
        meta:{title:"任务日志"},
        component: () => import('@/views/task_manage/task/Record')
      }
    ]
  },
  {
    path: "/batch",
    name: "批量执行",
    icon: "Document",
    component: Layout,
    children: [
       {
        path: '/batch/command',
        name: "执行命令",
        meta:{title:"执行命令"},
        component: () => import('@/views/batch_execution/batch/Command')
      },
       {
        path: '/batch/execution_record',
        name: "执行记录",
        meta:{title:"执行记录"},
        component: () => import('@/views/batch_execution/batch/ExecutionRecord')
       },
       {
        path: '/batch/file',
        name: "文件分发",
        meta:{title:"文件分发"},
        component: () => import('@/views/batch_execution/batch/File')
      }
    ]
  },
  {
    path: "/tools",
    name: "运维工具",
    icon: "Tools",
    component: Layout,
    children: [
      {
        path: '/tools/remind',
        name: "提醒管理",
        meta:{title:"提醒管理"},
        component: () => import('@/views/tool_manage/remind/Remind')
      },
       {
        path: '/tools/event',
        name: "事件管理",
        meta:{title:"事件管理"},
        component: () => import('@/views/tool_manage/event/Event')
      },
      {
        path: '/tools/fault',
        name: "故障管理",
        meta:{title:"故障管理"},
        component: () => import('@/views/tool_manage/fault/Fault')
      },
       {
        path: '/tools/password',
        name: "工具管理",
        meta:{title:"工具管理"},
        component: () => import('@/views/tool_manage/password/Password')
      },
    ]
  },
  {
    path: "/usermanage",
    name: "系统管理",
    icon: "User",
    component: Layout,
    meta:{title:"用户管理",role:['admin']},
    children: [
       {
        path: '/usermanage/user',
        name: "用户管理",
        meta:{title:"用户管理",role:['admin']},
        component: () => import('@/views/system_manage/user/User')
      },
      {
        path: '/usermanage/userlog',
        name: "登录日志",
        meta:{title:"登录日志",role:['admin']},
        component: () => import('@/views/system_manage/userlog/UserLog')
      },
      {
        path: '/usermanage/operatelog',
        name: "操作日志",
        meta:{title:"操作日志",role:['admin']},
        component: () => import('@/views/system_manage/userlog/OperateLog')
      }
    ]
  },
  {
    path: "/config",
    name: "系统配置",
    icon: "Setting",
    component: Layout,
    children: [
      {
        path: '/config/credential',
        name: "凭据管理",
        meta:{title:"凭据管理"},
        component: () => import('@/views/system_config/credential/Credential')
      },
      {
        path: '/config/notify',
        name: "通知管理",
        meta:{title:"通知管理"},
        component: () => import('@/views/system_config/notify/Notify')
      },
      {
        path: '/config/setting',
        name: "系统设置",
        meta:{title:"系统设置"},
        component: () => import('@/views/system_config/setting/Setting')
      },
    ]
  },

  // {
  //   path: "/nav3",
  //   name: "一级菜单3",
  //   component: Layout,
  //   children: [
  //     {
  //       path: '/nav3/d',
  //       name: "一级菜单3",
  //       icon: "UserFilled",
  //       component: () => import('../views/D')
  //     }
  //   ]
  // }

];

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),  //路由模式
  routes,
});

//导航守卫
router.beforeEach((to,from,next) => {
  // 在路由改变之前显示进度条
  NProgress.start();
  console.log(to.path,from.path,next);
  // 如果用户访问登录页，直接放行
  if(to.path == "/login") {
    return next();
  }
  const token = window.localStorage.getItem('token');
  if(token) {
       //判断当前用户角色是否有权限访问
      if (to.meta.role){
          //获取当前用户的角色
          let userRole = window.localStorage.getItem('role'); // 获取用户角色，默认1或2
          let UserRole = userRole === '1' ? 'admin' : 'user'; //获取用户角色名称
          //to.meta.role 允许的角色列表，['admin','user']
          let allowRoleList = to.meta.role;
          if(allowRoleList.indexOf(UserRole) === -1){
              next({name: '仪表盘'}); //如果角色没有权限跳转到仪表盘
          }else {
            next(); //如果角色有权限则正常跳转
          }
      }else {
        next(); //如果没配置菜单权限则正常跳转
      }
  } else {
    return next('/login') // 如果没有token则跳转到登录页
  }
  //判断是否有标题，动态设置标题
   if(to.meta.title){
        document.title = to.meta.title
    }else {
       next()  //执行进入路由，如果不写就不会进入目标页
   }
});
// 在路由改变之后隐藏进度条
router.afterEach(() => {
  NProgress.done()
})

export default router;
